package cn.jekun.shop.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.struts2.BaseAction;
import cn.jekun.component.core.bean.gen.ErpShoptype;
import cn.jekun.component.core.service.gen.DCityService;
import cn.jekun.component.core.service.gen.DProvinceService;
import cn.jekun.shop.common.service.ShopBaseAction;
import cn.jekun.shop.common.service.SysParamService;

/**
 * 系统参数
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class SysParamAction extends BaseAction{
	@Resource(name = DProvinceService.BEAN_NAME)
	protected DProvinceService provinceService;
	@Resource(name = DCityService.BEAN_NAME)
	protected DCityService cityService;
	@Resource(name = SysParamService.BEAN_NAME)
	protected SysParamService sysParamService;
	/**
	 * 列出所有的省份(仅提供分润模块)
	 * @return
	 */
	public String provinceList(){
		this.resultList = this.provinceService.listBySql("select * from D_PROVINCE",page);
		return "/admin/profit/selectProvince.jsp";
	}
	/**
	 * 列出所有的城市(仅提供分润模块)
	 * @return
	 */
	public String cityList(){
		
		this.resultList =  this.cityService.listBySql("select * from D_CITY",page);
		
		return "/admin/profit/selectCity.jsp";
		
	}
	
	
	/**
	 * 获取所有城市
	 * @return
	 */
	public String showAllCity(){
		
		
		JSONObject jsonObject = new JSONObject();
		Map formMap = new HashMap();
		List<Map> list=  this.cityService.listBySql("select * from D_CITY order by nlssort(PINYIN,'NLS_SORT=SCHINESE_PINYIN_M')");;
		
		List listData=new ArrayList();
		
		HashMap<String, Object> hashMap;
		
		List<String> pinyinList=new ArrayList<String>();
		for(int i=0;i<list.size();i++){
			
				if(!pinyinList.contains(list.get(i).get("PINYIN"))){
					pinyinList.add(list.get(i).get("PINYIN").toString());
					hashMap=new HashMap<String, Object>();
					hashMap.put("pinyin", list.get(i).get("PINYIN"));
					hashMap.put("childCityList", getChildType(list.get(i).get("PINYIN").toString(),list));
					listData.add(hashMap);
					
				}
			
		}
		jsonObject.put("cityList", listData);
		
		return this.writeJson(jsonObject);
	}
	
	
	public JSONArray getChildType(String pinyin,List<Map> list){
		
		JSONArray jsonArray=new JSONArray();
		
		
		for(int i=0;i<list.size();i++){
			
			if(pinyin.equals(list.get(i).get("PINYIN"))){
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("cityId", list.get(i).get("CITY_ID"));
				jsonObject.put("cityName", list.get(i).get("CITY_NAME"));
				jsonObject.put("pinyin", list.get(i).get("PINYIN"));
				jsonArray.add(jsonObject);
			}
			
		}
		
		return jsonArray;
		
	}
	
	
	
	/**
	 * 查找所有的省市
	 * @return json
	 */
	@SuppressWarnings("unchecked")
	public String findAllProvince(){
		List<String> list = this.provinceService.listBySql("select * from D_PROVINCE");
		JSONArray json = JSONArray.fromObject(list);
		return this.writeJson(json);
	}
	/**
	 * 查找省份下面所有的地市
	 * @return json
	 */
	@SuppressWarnings("rawtypes")
	public String findCityByProvince(){
		JSONArray jsonObject = null;
		try{
			String provinceId = this.request.getParameter("provinceId");
			List list =  this.sysParamService.finCityByProvince(provinceId);
			jsonObject  = JSONArray.fromObject(list);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONArray.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONArray.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		
	}
	/**
	 * 查找地市下所有的区
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String findRegionByCity(){
		JSONArray jsonObject = null;
		try{
		String cityId = this.request.getParameter("cityId");
		List list =  this.sysParamService.findRegionByCity(cityId);
		jsonObject = JSONArray.fromObject(list);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONArray.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONArray.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
	}
	/**
	 * 城市翻译
	 * @return
	 */
	public String cityTranslate(){
		
		return "json";
	}
	/**
	 * 省份翻译
	 * @return
	 */
	public String provinceTranslate(){
		
		return "json";
	}
}
